//
//  LFTabBar.m
//  College
//
//  Created by LF on 2018/11/26.
//  Copyright © 2018 MuXi. All rights reserved.
//

#import "LFTabBar.h"

@interface LFTabBar() <LFTabBarItemDelegate>
@property (nonatomic, strong) NSMutableArray *items; /**< items */
@end

@implementation LFTabBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];//[UIColor lf_colorWithHexString:@"#F0F2F5"]
    }
    return self;
}

+ (instancetype)sharedTabBar {
    static dispatch_once_t onceToken;
    static LFTabBar *tabbar;
    dispatch_once(&onceToken, ^{
        tabbar = [[LFTabBar alloc] initWithFrame:CGRectMake(0, kScreenH - tabBarHeight, kScreenW, tabBarHeight)];
    });
    return tabbar;
}

- (void) configTabbar:(NSArray *)datas {
    
    float w = kScreenW / datas.count;
    
    for (int i = 0; i < datas.count; i++) {
        NSDictionary *data = datas[i];
        LFTabBarItem *item = [[LFTabBarItem alloc] initWithFrame:CGRectMake(w * i, 0, w, 49)];
        item.delegate = self;
        item.tag = 100 + i;
        [item lf_setItem:data];
        item.selected = i == 0;
        [self addSubview:item];
        [self.items addObject:item];
    }
}
#pragma mark - hide & show
- (void)setDisplay:(Display)display {
    [UIView animateWithDuration:0.1 animations:^{
        CGFloat y = display == DisplayShow ? kScreenH - tabBarHeight : kScreenH;
        self.frame = CGRectMake(0, y, kScreenW, kScreenH);
    }];
}
#pragma mark - 设置数量
- (void) setMessageCount:(NSInteger)count atIndex:(NSInteger)index {
    LFTabBarItem *item = [self.items objectAtIndex:index];
    item.messageCount = count;
}
#pragma mark - LFTabBarItemDelegate
- (void)didSelectItem:(LFTabBarItem *)item {
    if (self.delegate && [self.delegate respondsToSelector:@selector(didSelectedTabBarAtIndex:)]) {
        [self.delegate didSelectedTabBarAtIndex:item.tag - 100];
        for (LFTabBarItem *item1 in self.subviews) {
            if ([item isKindOfClass:[LFTabBarItem class]] && item1 != item) {
                item1.selected = NO;
            }
        }
    }
}
#pragma mark - lazy
- (NSMutableArray *)items {
    if (!_items) {
        _items = [NSMutableArray array];
    }
    return _items;
}
@end
